package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.ReturnListService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.service.SaleListService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WZhaokang
 * @date 2020/12/30 1:20
 */
@RestController
@RequestMapping("/saleListGoods")
public class SaleListGoodsController {


    @Autowired
    private SaleListService saleListService;
    @Autowired
    private SaleListGoodsService saleListGoodsService;

    //请求URL：http://localhost:8080/saleListGoods/save?saleNumber=XS1605772263999
    @RequestMapping("/save")
    public ServiceVO save(SaleList saleList, String saleListGoodsStr, HttpSession session){
        //销售单的保存有两个特殊的属性
        //private String customerName;
        //  private String trueName;
        saleListService.save(saleList,saleListGoodsStr,session);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    //请求URL：http://localhost:8080/saleListGoods/list
    @RequestMapping("/list")
    public Map<String,List<SaleList>> list(String saleNumber, Integer customerId, Integer state, String sTime,String eTime){
        Map map = new HashMap();
        List<SaleList> returnListList = saleListService.list(saleNumber,customerId,state,sTime,eTime);
        map.put("rows",returnListList);
        return map;
    }

    //请求URL：http://localhost:8080/saleListGoods/goodsList
    @RequestMapping("/goodsList")
    public Map<String,List<SaleListGoods>> goodList(Integer saleListId){
        Map map = new HashMap();
        List<SaleListGoods> saleListGoodsList = saleListGoodsService.goodList(saleListId);
        map.put("rows",saleListGoodsList);
        return map;
    }


    //请求URL：http://localhost:8080/saleListGoods/delete
    @RequestMapping("/delete")
    public ServiceVO delete(Integer saleListId){
        saleListService.delete(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    //请求URL：http://localhost:8080/saleListGoods/updateState
    @RequestMapping("/updateState")
    public ServiceVO updateState(Integer saleListId){
        saleListService.updateState(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    //请求URL：http://localhost:8080/saleListGoods/count
    @RequestMapping("/count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        List<PurchaseVO2> purchaseVOList = saleListService.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String json = gson.toJson(purchaseVOList);
        return json;
    }

    //请求URL：http://localhost:8080/saleListGoods/getSaleDataByDay
    @RequestMapping("/getSaleDataByDay")
    public String getSaleDataByDay(String sTime, String eTime){
        List<Map> mapList = saleListService.getSaleDataByDay(sTime,eTime);
        Gson gson = new Gson();
        String json = gson.toJson(mapList);
        return json;
    }

//    请求URL：http://localhost:8080/saleListGoods/getSaleDataByMonth
    @RequestMapping("/getSaleDataByMonth")
    public String getSaleDataByMonth(String sTime,String eTime){
        List<Map> mapList = saleListService.getSaleDataByMonth(sTime,eTime);
        Gson gson = new Gson();
        String json = gson.toJson(mapList);
        return json;
    }
}
